﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com>
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// LogListView.cs:
//
// Classe que exibe o log do programa
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WBFSSync.AeroControls
{
    class LogListView : ListView
    {
        //----------------------- Constantes

        const int MaxLines = 512;

        //----------------------- Rotinas

        public LogListView() : base()
        {
            DoubleBuffered = true;
            View = View.Details;
            OwnerDraw = true;
            Columns.Add("");
        }

        public void AddLine(string line, LogMessageType type)
        {
            ListViewItem item = new ListViewItem(line);
            item.Tag = type;
            Items.Add(item);

            EnsureVisible(Items.Count - 1);
        }

        public void UpdateLastLine(String piece)
        {
            Items[Items.Count - 1].Text += piece;
            Invalidate(Items[Items.Count - 1].Bounds);

            EnsureVisible(Items.Count - 1);
        }

        protected override void OnResize(EventArgs e)
        {
            if (Columns.Count > 0) Columns[0].Width = Width - 17;
            base.OnResize(e);
        }

        protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.HighSpeed;
            e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            if ((e.ItemState & ListViewItemStates.Selected) != (ListViewItemStates)0)
            {
                e.Graphics.DrawImage(Skin.explorer_detail_selection_l_c, e.Bounds.X, e.Bounds.Y, 3, e.Bounds.Height);
                e.Graphics.DrawImage(Skin.explorer_detail_selection_m_c, e.Bounds.X + 3, e.Bounds.Y, e.Bounds.Width - 6, e.Bounds.Height);
                e.Graphics.DrawImage(Skin.explorer_detail_selection_r_c, e.Bounds.Right - 3, e.Bounds.Y, 3, e.Bounds.Height);
            }

            Image icon = null;

            switch ((LogMessageType)e.Item.Tag)
            {
                case LogMessageType.Error: icon = Skin.icon_16x16_log_error; break;
                case LogMessageType.Warning: icon = Skin.icon_16x16_log_warning; break;
                case LogMessageType.Message: icon = Skin.icon_16x16_log_message; break;
            }

            if (icon != null) 
                e.Graphics.DrawImage(icon, e.Bounds.Left + 2, e.Bounds.Top + (e.Bounds.Height - 16) / 2, 16, 16);

            StringFormat format = new StringFormat();
            format.Trimming = StringTrimming.EllipsisCharacter;
            format.LineAlignment = StringAlignment.Near;
            format.Alignment = StringAlignment.Near;

            RectangleF bounds = new RectangleF(e.Bounds.Left + 20,
                e.Bounds.Top + (e.Bounds.Height - Skin.IconTitleFont.Height) / 2, Columns[e.ColumnIndex].Width - 5,
                Skin.IconTitleFont.Height);

            e.Graphics.DrawString(e.Item.Text, Skin.IconTitleFont, Brushes.Black, bounds, format);

            base.OnDrawSubItem(e);
        }


    }
}

